package com.example.project_01.controller;

import com.example.project_01.commons.Result;
import com.example.project_01.dto.DrugDTO;
import com.example.project_01.model.DrugModel;
import com.example.project_01.service.DrugService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.RolesAllowed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/drug")
@CrossOrigin
@Tag(name = "药品信息接口", description = "这个接口是药品信息的接口")
public class DrugController {

    @Autowired
    DrugService drugService;

    //获取所有药品信息并分页，drugName不为空则模糊查询
    @GetMapping(value = {"/getDrug/{pn}/{size}", ""})
    @Operation(summary = "查询所有药品信息",description = "分页查询药品信息")
    public Result getDrugWithPage(@PathVariable(value = "pn",required = false) Integer pn,
                                  @PathVariable(value = "size",required = false) Integer size,
                                  @Parameter(
                                          name = "drugName",
                                          description = "药品名称，支持模糊查询，为空时查询所有药品信息",
                                          example = "布洛芬混悬液 (美林)",
                                          required = false,
                                          schema = @Schema(type = "string")
                                  )
                                  @RequestParam(value = "drugName",required = false) String drugName){

        //如果pn为空则默认第一页
        if (pn == null){
            pn = 1;
        }
        if (size == null){
            size = 10;   //默认每页10条
        }
        PageInfo<DrugModel> info =drugService.getDrugWithPage(pn,size,drugName);
        if (info != null) {
            return new Result(200,"药品信息查询成功！",info);
        }
        return new Result(201,"找不到药品信息的数据",null);
    }

    //新增药品信息
    //@RolesAllowed({"1","2"})   //指定访问的角色
    @PostMapping("/addDrug")
    @Operation(summary = "添加药品信息",description = "添加药品信息")
    public Result addDrug(@RequestBody DrugDTO drugDTO){
        return drugService.addDrug(drugDTO);
    }

    //删除药品信息
    @GetMapping("/delectDrug/{drugId}")
    @Operation(summary = "删除药品信息",description = "删除药品信息")
    public Result delectDrug(@PathVariable("drugId") Integer drugId){
        return drugService.deleteDrug(drugId);
    }

    //更新药品信息
    @PostMapping("/updateDrug")
    @Operation(summary = "更新药品信息",description = "更新药品信息")
    public Result updateDrug(@RequestBody DrugDTO drugDTO){
        return drugService.updateDrug(drugDTO);
    }







}
